﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VirtualPress.Infrastructure.Entities.Entity;

namespace VirtualPress.Infrastructure.Entities.Cache
{
    public class BookCache : EntitiesCache<Book>
    {
        protected override bool ValidateParameter(params string[] keys)
        {
            return keys.Length == 1 && !String.IsNullOrEmpty(keys[0]);
        }

        protected override Book LoadDataItem(params string[] keys)
        {
            try
            {
                int id = Int32.Parse(keys[0]);
                return new Book().Find(c=>c.Id ==id);
            }
            catch { }
            return null;
        }

        protected override string BuildKey(params string[] keys)
        {
            if (ValidateParameter(keys))
            {
                return String.Format("GetBookEntity_{0}", keys[0]);
            }
            return string.Empty;
        }

        protected override string BuildKey(Book data)
        {
            if (data != null)
            {
                return BuildKey(data.Id.ToString());
            }
            return string.Empty;
        }
    }
}
